﻿var online = new Array();

var CSPContactWays = {
    IsFinded: false,
    ContactWays: null, /*联系方式列表  [{"Address":"619381","Type":1,"UserName":Admin},{"Address":"hl_8792","Type":0,"UserName":Admin}]*/
    GetImgURL: function (contactWayType, onlineStatus) { /*contactWayType 0 代表阿里旺旺 1代表qq ; onlineStatus 0 不在线 1在线*/
        //旺旺不在线
        if (contactWayType == 0 && onlineStatus == 0) {
            return "/Images/contact/WangWangOffLine.gif";
        }
        //旺旺在线
        else if (contactWayType == 0 && onlineStatus == 1) {
            return "/Images/contact/WangWangOnLine.gif";
        }
        //QQ不在线
        if (contactWayType == 1 && onlineStatus == 0) {
            return "/Images/contact/QQOffLine.gif";

        }
        //QQ在线
        if (contactWayType == 1 && onlineStatus == 1) {
            return "/Images/contact/QQOnLine.gif";

        }
    },
    GetLinkURL: function (contactWayType, address) {/*contactWayType 0 代表阿里旺旺 1代表qq ;address 代表请求的地址*/
        var link = "";
        if (contactWayType == 0) {
            link = "http://" + "www.taobao.com/webww/ww.php?ver=3&touid=" + encodeURI(address) + "&siteid=cntaobao&status=1&charset=utf-8";
        }
        else {
            link = "http://" + "wpa.qq.com/msgrd?v=3&amp;uin=" + encodeURI(address) + "&amp;site=qq&amp;menu=yes";

        }
        return link;
    },
    Init: function () {
        this.RequestApi();
    },
    RequestApi: function () {
        var that = this;
        var taobaoi = 0;
        var qqFilePath = 'http://' + 'webpresence.qq.com/getonline?' + encodeURI('Type=1&');
        var taobaoFilePath = 'http://' + 'amos.alicdn.com/muliuserstatus.aw?beginnum=0&site=cntaobao&charset=utf-8&callback=CSPContactWays.CreateTaoBaoContact&uids=';
        $.each(this.ContactWays, function (idex, item) {
            /*1代表是QQ*/
            if (item.Type == 1) {
                qqFilePath += item.Address + ":";
            }
            /*0代表淘宝*/
            if (item.Type == 0) {
                if (taobaoi == 0) {
                    taobaoFilePath += encodeURI(item.Address);
                } else {
                    taobaoFilePath += ";" + encodeURI(item.Address);
                }

                taobaoi++;
            }
        });

        //异步处理QQ
        $.ajax({
            type: "get",
            url: qqFilePath,
            cache: false,
            dataType: "script",
            async: false,
            error: function (XMLHttpRequest, textStatus, errorThrown) {

            },
            success: function (data, textStatus, jqXHR) {
                that.CreateQQContact();
            }
        });

        $.ajax({
            type: "get",
            url: taobaoFilePath,
            cache: false,
            dataType: "script",
            async: false,
            error: function (XMLHttpRequest, textStatus, errorThrown) {

            },
            success: function (data, textStatus, jqXHR) {
                
            }
        });
    },
    CreateTaoBaoContact: function (taobaoResponse) {

        for (var i = 0; i < taobaoResponse.data.length; i++) {
            if (taobaoResponse.data[i] == 1) {
                var contactWay = this.GetDataByTaoBao(i);

                var contacts = $(".contact[name='" + contactWay.UserName + "']");

                if (contacts.find("img").eq(0).attr("src").indexOf('?') != -1) {
                    contacts.find("img").attr("src", this.GetImgURL(0, 1));
                    contacts.find("a").attr("href", this.GetLinkURL(0, contactWay.Address));
                }

                //return;
            }
        }
        this.IsFinded = true;

    },
    CreateQQContact: function () {
        for (var i = 0; i < online.length; i++) {
            /*online*/
            if (online[i] == 1) {
                var contactWay = this.GetDataByQQ(i);

                var contacts = $(".contact[name='" + contactWay.UserName + "']");

                if (contacts.find("img").eq(0).attr("src").indexOf('?') != -1) {
                    contacts.find("img").attr("src", this.GetImgURL(1, 1));
                    contacts.find("a").attr("href", this.GetLinkURL(1, contactWay.Address));
                }

                //return;
            }
        }
        this.IsFinded = true;
    },
    GetDataByTaoBao: function (itemIndex) {/*返回值 {"Address":"619381","Type":1,"UserName":Admin}*/
        var result = null;
        var i = 0;
        $.each(this.ContactWays, function (index, item) {
            /*0代表是淘宝*/
            if (item.Type == 0) {
                if (itemIndex == index) {
                    result = item;
                    return;
                }
                i++;
            }

        });
        return result;
    },
    GetDataByQQ: function (itemIndex) {/*返回值 {"Address":"619381","Type":1,"UserName":Admin}*/
        var result = null;
        var i = 0;
        $.each(this.ContactWays, function (index, item) {
            /*1代表是QQ*/
            if (item.Type == 1) {
                if (itemIndex == index) {
                    result = item;
                    return;
                }
                i++;
            }

        });
        return result;
    }

};